﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class StockController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IStockRepository StockRepository;
        private readonly ICompanyRepository companyRepository;
        public StockController(ICommandBus commandBus, IStockRepository StockRepository,ICompanyRepository companyRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.StockRepository = StockRepository;
            this.companyRepository = companyRepository;
        }
        //
        // GET: /Stock/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = StockRepository.GetAll();
            ViewData["Company"] = companyRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(StockFormModels frm)
        {
            var form = new StockFormModels()
            {
                ID = frm.ID,
                Address=frm.Address,
                Code=frm.Code,
                CurrentCompanyID=frm.CurrentCompanyID,
                Description=frm.Description,
                MaxQuatity=frm.MaxQuatity,
                Name=frm.Name,
                Type=frm.Type

            };
            var command = Mapper.Map<StockFormModels, CreateOrUpdateStockCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = StockRepository.GetAll();
            return PartialView("_StockList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            Stock product = StockRepository.GetById(ID);
            var obj = Mapper.Map<Stock, StockFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteStockCommand> list = new List<DeleteStockCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteStockCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = StockRepository.GetAll();
            return PartialView("_StockList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Stock> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = StockRepository.GetAll();
            }
            else
            {
                model = StockRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Description.Contains(Name)) || (x.Address.Contains(Name)) || (x.Code.Contains(Name)));

            }
            return PartialView("_StockList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = StockRepository.GetAll();
            return PartialView("_StockList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
